<%@ page language="java" contentType="text/html; charset=UTF-8"
		 pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<%String ctx = request.getContextPath();%>
	<title>scoreset list</title>
</head>

<script>
	$(document).ready(function(){
		$.ajax(
				{
					dataType: "json",
					type: "GET",
					url: "<%=ctx%>/paper?act=getAll",
					data: {},
					success: function(data, status) {
						let select = $("#paper")
						select.empty();
						let idx = 1;
						$.each(data, function(i, e) {
							select.append("<option value='" + e.id + "'>"+ e.name +"</option>");
							idx += 1;
						})
					},
					error: function(req, status, msg) {
						alert(msg)
					}
				}
		)
	});

	function createScoreSet() {
		if(confirm('确定新建?')) {
			$.ajax(
					{ type:"POST",
						url: "<%=ctx%>/scoreSet?act=addScoreSet",
						success: function(data, status) {
							alert(data);
							AjaxLoad('<%=ctx%>/control?act=scoreSetOpera');
						},
						error: function(req, status, msg) {
							alert(msg)
						}
					}
			)
		}
	}

	function deleteScoreSet(id) {
		if(confirm('确定删除?')) {
			$.ajax(
					{ type:"POST",
						url: "<%=ctx%>/scoreSet?act=deleteScoreSet",
						data: {
							"setid" :id
						},
						success: function(data, status) {
							alert(data);
							AjaxLoad('<%=ctx%>/control?act=scoreSetOpera');
						},
						error: function(req, status, msg) {
							alert(msg)
						}
					}
			)
		}
	}

	function updateScoreSet(id) {
		if(confirm('确定更新?')) {
			$.ajax(
					{ type:"POST",
						url: "<%=ctx%>/scoreSet?act=updateScoreSet",
						data: {
							"setid" :id,
							"c" : $("#cset").val(),
							"m" : $("#mset").val(),
							"j" : $("#jset").val(),
							"d" : $("#dset").val(),
						},
						success: function(data, status) {
							alert(data);
							AjaxLoad('<%=ctx%>/control?act=scoreSetOpera');
						},
						error: function(req, status, msg) {
							alert(msg);
						}
					}
			)
		}
	}


	function addToPaper(id) {

		let paper = $("#paper").find("option:selected");
		let paperName = paper.text();
		let paperId = paper.val();

		if(confirm("关联到 "+paperName+"?") !== true)
			return;

		$.ajax(
				{
					type:"GET",
					url: '<%=ctx%>/scoreSet?act=addScoreSetToPaper',
					data: {
						"setid": id,
						"paperid":paperId,
					},
					success: function(data, status) {
						alert(data);
					},
					error: function(req, status, msg) {
						alert(msg);
					}
				}
		)
	}

</script>


<body>
<div id="list" class="col-md-10 col-xs-12">
	<h3>分值设定</h3>
	<hr>
	<div class="fiter col-md-10 col-xs-12">
		<button type="button" class="btn btn-success" onclick="createScoreSet()">新建</button>
	</div>

	<div class="fiter col-md-10 col-xs-12">
		<h5>选择一个考试</h5>
		<form action="" class="form-horizontal form-inline">
			<select class="form-control" id="paper"></select>
		</form>
	</div>

	<table class="table table-hover">
		<thead> <tr>
			<th>序号</th>
			<th style="width: 100px">单选</th>
			<th style="width: 100px">多选</th>
			<th style="width: 100px">判断</th>
			<th style="width: 100px">简答</th>
			<th>操作</th>
		</tr></thead>

		<tbody id="setbody">
		<c:forEach items="${SetList}" var="Set" varStatus="s">
			<tr>
				<td>${s.index + 1}</td>
				<td><input class="form-control half" type="text" id="cset" value="${Set.choiceScore}"></td>
				<td><input class="form-control half" type="text" id="mset" value="${Set.multichoiceScore}"></td>
				<td><input class="form-control half" type="text" id="jset" value="${Set.judgeScore}"></td>
				<td><input class="form-control half" type="text" id="dset" value="${Set.discussScore}"></td>
				<td>
					<button type="button" class="btn btn-success" onclick="addToPaper('${Set.id}')">
						关联到试卷</button>
					<button type="button" class="btn btn-primary" onclick="updateScoreSet('${Set.id}')">
						更新</button>
					<button type="button" class="btn btn-danger" onclick="deleteScoreSet('${Set.id}')">
						删除</button>
				</td>
			</tr>
		</c:forEach>
		</tbody>
	</table>
</div>
</body>
</html>